<template>

  <!-- 搜索框 -->
  <el-card style="height: 80px">
    <el-form :model="queryForm" :inline="true" class="form">
      <el-form-item label="用户名称:">
        <el-input
            v-model="queryForm.queryUserName"
            placeholder="请输入用户名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="教练名称:" v-if="userStore.role===1">
        <el-input
            v-model="queryForm.queryTrainerName"
            placeholder="请输入教练名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="课程名称:">
        <el-input
            v-model="queryForm.queryTitle"
            placeholder="请输入课程名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态:">
        <el-select
            v-model="queryForm.queryState"
            placeholder="请选择状态"
            style="width: 100px"
            filterable
        >
          <el-option label="全部" :value="0"></el-option>
          <el-option label="待确认" :value="2"></el-option>
          <el-option label="已确认" :value="1"></el-option>
          <el-option label="已取消" :value="3"></el-option>
          <el-option label="已拒绝" :value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default" @click="search">
          搜索
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>


  <!-- 添加用户和批量删除 -->
  <el-card style="margin: 10px 0">
    <!-- table展示用户信息 -->
    <el-table
        style="margin: 10px 0"
        border
        :data="bookingsTableData"
    >
      <el-table-column
          label="用户名称"
          align="center"
          prop="userName"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column
          label="教练名称"
          align="center"
          prop="trainerName"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column
          label="课程名称"
          align="center"
          prop="title"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column
          label="预约时间"
          align="center"
          prop="bookingTime"
          :formatter="formatDate"
          show-overflow-tooltip
      ></el-table-column>
      <el-table-column
          label="状态"
          align="center"
          prop="status"
      >
        <template #="{ row }">
          <el-tag v-if="row.status === 1" type="success">已确认</el-tag>
          <el-tag v-else-if="row.status === 2" type="warning">待确认</el-tag>
          <el-tag v-else-if="row.status === 3" type="info">已取消</el-tag>
          <el-tag v-else-if="row.status === 4" type="danger">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="120px" align="center">
        <template #="{ row }">
          <el-tooltip effect="dark" content="确认" placement="top">
            <el-button
                type="success"
                size="small"
                icon="Sell"
                @click="handleEditState(row,1)"
            >
            </el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="拒绝" placement="top">
            <el-button
                type="warning"
                size="small"
                icon="SoldOut"
                @click="handleEditState(row,4)"
            >
            </el-button>
          </el-tooltip>
        </template>

      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :background="true"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="total"
        @current-change="findAll"
        @size-change="handler"
    />
  </el-card>


</template>

<script setup lang="ts">
import {ref, reactive, onMounted} from 'vue'
import {ElFormItem, ElMessage, ElMessageBox} from 'element-plus'
import {apiGet, apiPost} from "@/utils/api.ts";
import {formatDate} from "@/utils/time.ts";
//获取用户相关
import { useUserStore } from '@/store/modules/user'

let userStore = useUserStore()
//搜索表单数据
let queryForm = reactive({
  queryUserName: '',
  queryTrainerName: '',
  queryState: 0,
  queryTitle: '',
  trainerId: 0,
})

//默认页码
let pageNo = ref<number>(1)
//一页展示几条数据
let pageSize = ref<number>(5)
//总个数
let total = ref<number>(0)
//存储全部用户的数组
let bookingsTableData = ref([])

//获取全部用户信息
let findAll = async (pager = 1) => {
  //收集当前页码
  pageNo.value = pager
  let parameter = {
    page: pageNo.value,
    limit: pageSize.value,
    userName: queryForm.queryUserName,
    trainerName: queryForm.queryTrainerName,
    status: queryForm.queryState,
    title: queryForm.queryTitle,
    trainerId: userStore.role === 1 ? queryForm.trainerId : userStore.id
  }
  //获取表单数据
  let {data} = await apiGet('/bookings/findAll', parameter)
  total.value = data.count
  bookingsTableData.value = data.data
}

//分页器下拉菜单的自定义事件的回调
const handler = () => {
  findAll()
}

//搜索按钮的回调
const search = () => {
  findAll()
}

//发布按钮和停止按钮
const handleEditState = async (row: any, type: number) => {
  const oldType = row.status
  if (type === 1) {
    if (oldType !== 2) {
      ElMessage.warning('只能对待确认的预约进行确认')
      return
    }
  } else if (type === 4) {
    if (oldType !== 2) {
      ElMessage.warning('只能对待确认的预约进行拒绝')
      return
    }
  } else {
    ElMessage.error('请选择正确的操作')
    return
  }
  ElMessageBox.confirm('此操作将改变预约状态, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(async () => {
        await apiPost('/bookings/editState', {bookingId: row.bookingId, status: type})
        await findAll()
      })
      .catch(() => {
      })
}


//组件挂载完毕
onMounted(() => {
  findAll()
})
</script>

<style scoped>
</style>
